package mulMan.gui.view;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import mulMan.domain.AudioFile;

public abstract class AudioFileView extends JFrame  implements Observer {

	private static final long serialVersionUID = -1833973059123903566L;
	
	private AudioFile audioFile;
	private JLabel titleLabel;
	private JTextField titleTextField;
	private JLabel albumLabel;
	private JTextField albumTextField;
	private JLabel artistLabel;
	private JTextField artistTextField;
	private JLabel lengthLabel;
	private JTextField lengthTextField;
	private JLabel pathLabel;
	private JTextField pathTextField;
	private JButton buttonFind;
	private JFileChooser chooser;
	private JPanel panel;
	private JLabel errorLabel;

	public AudioFile getAudioFile() {
		return audioFile;
	}
	public void setAudioFile(AudioFile audioFile) {
		this.audioFile = audioFile;
	}
	public JTextField getTitleTextField() {
		return titleTextField;
	}
	public void setTitleTextField(JTextField titleTextField) {
		this.titleTextField = titleTextField;
	}
	public JTextField getAlbumTextField() {
		return albumTextField;
	}
	public void setAlbumTextField(JTextField albumTextField) {
		this.albumTextField = albumTextField;
	}
	public JTextField getArtistTextField() {
		return artistTextField;
	}
	public void setArtistTextField(JTextField artistTextField) {
		this.artistTextField = artistTextField;
	}
	public JTextField getLengthTextField() {
		return lengthTextField;
	}
	public void setLengthTextField(JTextField lengthTextField) {
		this.lengthTextField = lengthTextField;
	}
	public JTextField getPathTextField() {
		return pathTextField;
	}
	public void setPathTextField(JTextField pathTextField) {
		this.pathTextField = pathTextField;
	}
	public JFileChooser getChooser() {
		return chooser;
	}
	public void setChooser(JFileChooser chooser) {
		this.chooser = chooser;
	}
	public JPanel getPanel() {
		return panel;
	}
	public void setPanel(JPanel panel) {
		this.panel = panel;
	}
	
	public JLabel getTitleLabel() {
		return titleLabel;
	}
	public JLabel getAlbumLabel() {
		return albumLabel;
	}
	public JLabel getArtistLabel() {
		return artistLabel;
	}
	public JLabel getLengthLabel() {
		return lengthLabel;
	}
	public JLabel getPathLabel() {
		return pathLabel;
	}
	public JLabel getErrorLabel() {
		return errorLabel;
	}
	public void setErrorLabel(JLabel errorLabel) {
		this.errorLabel = errorLabel;
	}
	
	
	
	public AudioFileView(AudioFile audioFile) {
		this.audioFile = audioFile;
		this.audioFile.addObserver(this);
		
		initView();
	    initFields();
	}

	public void update(Observable o, Object arg) {
		if (arg == "title") {
			titleTextField.setText(audioFile.getTitle());
		}
		if (arg == "album") {
			albumTextField.setText(audioFile.getAlbum());
		}
		if (arg == "artist") {
			artistTextField.setText(audioFile.getArtist());
		}
		if (arg == "length") {
			lengthTextField.setText(String.valueOf(audioFile.getLength()));
		}
		if (arg == "path") {
			pathTextField.setText(audioFile.getPath());
		}
	}

	public void initView() {
		panel = new JPanel();
	    getContentPane().add(panel);
	    panel.setLayout(null);
	       
	    
		setTitle("Audio file window");
	    setSize(250, 185);
	    setResizable(false);
	    setLocationRelativeTo(null);
	    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    setVisible(true);
	    
	    titleLabel = new JLabel();
	    titleLabel.setText("title: ");
	    titleLabel.setBounds(5, 5, 80, 20);
	    panel.add(titleLabel);
	    titleTextField = new JTextField();
	    titleTextField.setBounds(50, 5, 190, 20);
	    panel.add(titleTextField);
	    
	    albumLabel = new JLabel();
	    albumLabel.setText("album: ");
	    albumLabel.setBounds(5, 25, 80, 20);
	    panel.add(albumLabel);
	    albumTextField = new JTextField();
	    albumTextField.setBounds(50, 25, 190, 20);
	    panel.add(albumTextField);
	    
	    artistLabel = new JLabel();
	    artistLabel.setText("artist: ");
	    artistLabel.setBounds(5, 45, 80, 20);
	    panel.add(artistLabel);
	    artistTextField = new JTextField();
	    artistTextField.setBounds(50, 45, 190, 20);
	    panel.add(artistTextField);
	    
	    lengthLabel = new JLabel();
	    lengthLabel.setText("length: ");
	    lengthLabel.setBounds(5, 65, 80, 20);
	    panel.add(lengthLabel);
	    lengthTextField = new JTextField();
	    lengthTextField.setBounds(50, 65, 190, 20);
	    panel.add(lengthTextField);
	    
	    pathLabel = new JLabel();
	    pathLabel.setText("path: ");
	    pathLabel.setBounds(5, 85, 80, 20);
	    panel.add(pathLabel);
	    pathTextField = new JTextField();
	    pathTextField.setBounds(50, 85, 165, 20);
	    pathTextField.setEditable(false);
	    panel.add(pathTextField);
	    
	    buttonFind = new JButton();
	    buttonFind.setText("...");
	    buttonFind.setBounds(220, 85, 19, 20);
	    panel.add(buttonFind);
	    
	    chooser = new JFileChooser();
	    WavFileFilter filter = new WavFileFilter();
	    chooser.setFileFilter(filter);
	    
	    errorLabel = new JLabel();
	    errorLabel.setText("");
	    errorLabel.setForeground(Color.RED);
	    errorLabel.setBounds(5, 105, 235, 20);
	    panel.add(errorLabel);
	}
	
	public void initFields() {
		titleTextField.setText(audioFile.getTitle());
	    albumTextField.setText(audioFile.getAlbum());
	    artistTextField.setText(audioFile.getArtist());
	    lengthTextField.setText(String.valueOf(audioFile.getLength()));
	    pathTextField.setText(audioFile.getPath());
	}
	
	
	public void addFindAudioFileListener(ActionListener fafl) {
		buttonFind.addActionListener(fafl);
    }
	
	public void resetErrors() {
		getTitleLabel().setForeground(Color.BLACK);
		getAlbumLabel().setForeground(Color.BLACK);
		getArtistLabel().setForeground(Color.BLACK);
		getLengthLabel().setForeground(Color.BLACK);
		getPathLabel().setForeground(Color.BLACK);
		
		getErrorLabel().setText("");
	}
	
	
	
	
	
	public class WavFileFilter extends FileFilter {
		
		@Override
		public boolean accept(File f) {
			if (f.isDirectory()) {
		        return true;
		    }

			String fName = f.getName();
			String extension = "";
			int mid= fName.lastIndexOf(".");
			extension=fName.substring(mid+1,fName.length());
			
		    if (extension != null) {
		        if (extension.equals("wav")) {
		                return true;
		        } else {
		            return false;
		        }
		    }

		    return false;
		}

		@Override
		public String getDescription() {
			return "wav files only";
		}
		
	}
}
